package com.tanhua.server.service;

import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVO;
import com.tanhua.domain.vo.MessageCommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liuyp
 * @date 2021/02/04
 */
@Service
public class MessageService {
    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity addContact(Long targetUserId) {
        //1. 把好友关系保存到MongoDB
        friendApi.addFriend(UserHolder.getUserId(), targetUserId);

        //2. 把好友关系注册到环信
        huanXinTemplate.addContactUser(UserHolder.getUserId(), targetUserId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findContactList(int page, int pagesize, String keyword) {
        //1. 查询好友列表
        PageResult<Friend> pageResult = friendApi.findFriendList(UserHolder.getUserId(), keyword, page, pagesize);

        //2. 转换成VO
        List<Friend> friendList = pageResult.getItems();
        List<ContactVO> voList = new ArrayList<>();
        for (Friend friend : friendList) {
            ContactVO vo = new ContactVO();

            vo.setUserId(friend.getFriendId().toString());

            UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
            BeanUtils.copyProperties(userInfo, vo);

            String city = StringUtils.substringBefore(userInfo.getCity(), "-");
            vo.setCity(city);

            voList.add(vo);
        }

        //3. 构造返回值
        PageResult<ContactVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);

        return ResponseEntity.ok(voPageResult);
    }

    /**
     * 查询针对某一用户的操作列表
     * @param page
     * @param pagesize
     * @param commentType 操作类型。1点赞，2评论，3喜欢
     */
    public ResponseEntity findCommentList(int page, int pagesize, int commentType) {
        //1. 调用API，从MongoDB里分页查询针对当前用户的操作列表
        PageResult<Comment> pageResult = commentApi.findCommentListByType(page, pagesize, UserHolder.getUserId(), commentType);

        //2. 转换成VO
        List<Comment> commentList = pageResult.getItems();
        List<MessageCommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            MessageCommentVO vo = new MessageCommentVO();
            
            //查询评论人的信息
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setId(comment.getUserId().toString());

            String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(comment.getCreated()));
            vo.setCreateDate(dateStr);

            voList.add(vo);
        }

        //3. 构造返回值
        PageResult<MessageCommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }
}
